package com.peppa.petshop.controller;


import com.peppa.petshop.entity.Customer;
import com.peppa.petshop.service.CustomerService;
import com.peppa.petshop.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Controller
@RequestMapping({"/user", "/manage"})
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @ResponseBody
    public String msg(){
        return "用户名或密码错误";
    }
    @GetMapping(value = "/login")
    public ModelAndView login(){
        System.out.println("显示登录界面");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/login");
        return modelAndView;
    }

    @PostMapping(value = "/login")
    @ResponseBody
    @CrossOrigin
    public String login1(HttpServletRequest request, HttpServletResponse response, @RequestParam("phone") String phone,
                         @RequestParam("password")  String password){
        System.out.println("爷来了");
        System.out.println(phone);
        System.out.println(password);
        HttpSession session = request.getSession();
        Customer customer = customerService.login(phone, password);
        if (customer==null){
            System.out.println("登录失败");
            return "false";
        }else if(customer.getIsManager()){
            session.setAttribute("customerId",customer.getCustomerId());
            return "redirect:/index.html";
        }else{
//            session.setAttribute("customerId",customer.getCustomerId());
            Cookie cookie1=new Cookie("customerId",customer.getCustomerId().toString());
            System.out.println("hashCode():"+customer.getCustomerId().hashCode());
            Cookie cookie2=new Cookie("token", UUID.randomUUID().toString().replace("-",""));
            cookie1.setMaxAge(60*60);
            cookie1.setMaxAge(60*60);
            response.addCookie(cookie1);
            response.addCookie(cookie2);
            session.setAttribute("customerId",customer.getCustomerId());
            System.out.println(customer);
            response.setHeader("Access-Control-Allow-Credentials", "true");
            return "true";
        }
    }
    @ResponseBody
    @RequestMapping(value = "/list1")
    public DataVO<Customer> categoryList1(Integer page,Integer limit){
        return customerService.customerList( page, limit);
    }

    @RequestMapping(value = "/list")
    public ModelAndView categoryList(Integer page,Integer limit){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("customer");
        return modelAndView;
    }
    @GetMapping(value = "/del")
    public String del(Integer id){
        System.out.println("锁定用户");
        customerService.deleteById(id);
        return "redirect:/customer.html";
    }
    @GetMapping(value = "/unlock")
    public String unlock(Integer id){
        System.out.println("用户解锁");
        customerService.unlockById(id);
        return "redirect:/customer.html";
    }

    @ResponseBody
    @GetMapping(value = "/reset")
    public DataVO reset(Integer id){
        System.out.println("重置密码");
        customerService.resetById(id);
        DataVO<Object> dataVO = new DataVO<>(0,"ok",0,null);
        return dataVO;
    }

}

